package com.LearnInWorld.video.controller;

import com.LearnInWorld.entity.PageResult;
import com.LearnInWorld.entity.Result;
import com.LearnInWorld.entity.StatusCode;
import com.LearnInWorld.video.pojo.Video;
import com.LearnInWorld.video.service.VideoServoce;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private VideoServoce videoServoce;


    /***
     * 新增数据
     * @param video
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Video video) {
        videoServoce.add(video);
        return new Result(true, StatusCode.OK, "添加成功");
    }


    /***
     * 修改数据
     * @param video
     * @param id
     * @return
     */
    @PutMapping(value = "/up/{id}")
    public Result update(@RequestBody Video video, @PathVariable("id") String id) {
        video.setId(id);
        videoServoce.update(video);
        return new Result(true, StatusCode.OK, "修改成功");
    }


    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/del/{id}")
    public Result delete(@PathVariable("id") String id) {
        videoServoce.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }


    /**
     * 查询所有video数据
     *
     * @return
     */
    @GetMapping
    public Result findAll() {
        List<Video> videoList = videoServoce.findALl();
        return new Result(true, StatusCode.OK, "查询成功", videoList);
    }

    /**
     * 通过ID查询video数据
     *
     * @param videoId
     * @return
     */
    @GetMapping("/findById/{videoId}")
    public Result findById(@PathVariable("videoId") String videoId) {
        Video video = videoServoce.findById(videoId);
        return new Result(true, StatusCode.OK, "查询成功", video);
    }

    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Video> pageList = videoServoce.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }
}
